package BehaviorPattern.Strategy;

/**
 * @author: Themberfue
 * @date: 2025/8/13 11:09
 * @description:
 */
// 1. 抽象策略：支付方式接口
interface PaymentStrategy {
    void pay(double amount);
    String getPaymentName();
}

// 2. 具体策略：信用卡支付
class CreditCardPayment implements PaymentStrategy {
    private String cardNumber;
    private String name;
    private String cvv;
    private String expiryDate;

    public CreditCardPayment(String cardNumber, String name, String cvv, String expiryDate) {
        this.cardNumber = cardNumber;
        this.name = name;
        this.cvv = cvv;
        this.expiryDate = expiryDate;
    }

    @Override
    public void pay(double amount) {
        System.out.println(amount + "元 已通过信用卡支付");
        System.out.println("卡号: " + maskCardNumber(cardNumber));
        System.out.println("持卡人: " + name);
    }

    @Override
    public String getPaymentName() {
        return "信用卡";
    }

    // 隐藏卡号中间部分
    private String maskCardNumber(String cardNumber) {
        return cardNumber.substring(0, 4) + " **** **** " + cardNumber.substring(cardNumber.length() - 4);
    }
}

// 2. 具体策略：支付宝支付
class AlipayPayment implements PaymentStrategy {
    private String account;

    public AlipayPayment(String account) {
        this.account = account;
    }

    @Override
    public void pay(double amount) {
        System.out.println(amount + "元 已通过支付宝支付");
        System.out.println("账号: " + account);
    }

    @Override
    public String getPaymentName() {
        return "支付宝";
    }
}

// 2. 具体策略：微信支付
class WechatPayment implements PaymentStrategy {
    private String openId;

    public WechatPayment(String openId) {
        this.openId = openId;
    }

    @Override
    public void pay(double amount) {
        System.out.println(amount + "元 已通过微信支付");
        System.out.println("OpenID: " + openId);
    }

    @Override
    public String getPaymentName() {
        return "微信支付";
    }
}

// 3. 环境类：订单支付
class Order {
    private double amount;
    private PaymentStrategy paymentStrategy;

    public Order(double amount) {
        this.amount = amount;
    }

    // 设置支付策略
    public void setPaymentStrategy(PaymentStrategy paymentStrategy) {
        this.paymentStrategy = paymentStrategy;
    }

    // 执行支付
    public void pay() {
        if (paymentStrategy == null) {
            System.out.println("请选择支付方式");
            return;
        }
        System.out.println("=== 使用" + paymentStrategy.getPaymentName() + "支付 ===");
        paymentStrategy.pay(amount);
        System.out.println("支付完成\n");
    }
}

// 4. 客户端代码
public class StrategyExample {
    public static void main(String[] args) {
        // 创建订单
        Order order = new Order(299.99);

        // 选择信用卡支付
        PaymentStrategy creditCard = new CreditCardPayment(
                "6222123456789012",
                "张三",
                "123",
                "12/28"
        );
        order.setPaymentStrategy(creditCard);
        order.pay();

        // 切换为支付宝支付
        PaymentStrategy alipay = new AlipayPayment("zhangsan@example.com");
        order.setPaymentStrategy(alipay);
        order.pay();

        // 切换为微信支付
        PaymentStrategy wechat = new WechatPayment("o6_bmjrPTlm6_2sgVt7hMZOPfL2M");
        order.setPaymentStrategy(wechat);
        order.pay();
    }
}
